package auctionClient;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPListener implements Runnable{
	private int udpPort;
	private DatagramSocket datagramSocket = null;
	public UDPListener(int udpPort){
		this.udpPort=udpPort;
	}
		@Override
		public void run() {		
	        try {
				datagramSocket = new DatagramSocket(udpPort);
			} catch (SocketException e) {
				e.printStackTrace();
			}
	            byte[] receiveData = new byte[1024];
	         
	            while(true)
	               {
	                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
	                  try {
						datagramSocket.receive(receivePacket);
					} catch (IOException e) {
						break;
					}
	                  String sentence = new String( receivePacket.getData());
	                  System.out.println("\n" + sentence);
	               }	
			
		}
		

		public void close(){
			datagramSocket.close();
		}
}
